/**
 * 
 */
package com.yidu.erp.action.ware;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yidu.common.ExcelUtil;
import com.yidu.common.Pages;
import com.yidu.common.SsmMessage;
import com.yidu.erp.action.ware.vo.WareVo;
import com.yidu.erp.model.BeyondDep;
import com.yidu.erp.model.BeyondWare;
import com.yidu.erp.service.ware.WareService;

/**
 * 类的描述：药品的Action
 * @author xym
 *
 * @date 2017年11月29日
 */
@Controller
@RequestMapping("ware")
public class WareAction {
	/**
	 * 药品的service
	 */
	@Resource
	private WareService wareService;
	/**
	 * 显示药品的方法
	 * @author xym
	 * @param map 传入数据。
	 * @param pages 页面对象
	 * @param page 当前页数
	 * @param limit 每页显示数量
	 * @return 查询结果。总行数。
	 */
	@RequestMapping("showAll")
	@ResponseBody
	public Map<String, Object> showAll(@RequestParam Map<String, Object> map,Pages pages,int page, int limit){
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		map.put("pages", pages);
		return wareService.showAll(map);
	}
	/**
	 * 删除药品的方法
	 * @author xym
	 * @param ware 药品的对象
	 * @return 消息类
	 */
	@RequestMapping("delete")
	@ResponseBody
	public SsmMessage delete(BeyondWare ware){
		return wareService.delete(ware);
	}
	/**
	 * 增加药品的方法
	 * @author xym
	 * @param ware 药品的对象
	 * @return 消息对象
	 */
	@RequestMapping("insert")
	@ResponseBody
	public SsmMessage insert(BeyondWare ware){
		return wareService.insert(ware);
	}
	
	/**
	 * 根据id查药品的方法
	 * @author xym
	 * @param wareId 药品的编号
	 * @return 药品的视图对象
	 */
	@RequestMapping("findById")
	@ResponseBody
	public WareVo findById(String wareId){
		return wareService.findByIdVo(wareId);
	}
	/**
	 * 药品增加或修改的方法
	 * @author xym
	 * @param ware 药品对象
	 * @param dep 配料对象的字符串
	 * @param file 上传文件的路径
	 * @return 消息对象
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public SsmMessage addOrUpdate(BeyondWare ware,String dep,@RequestParam MultipartFile file){
		try {
			return wareService.addOrUpdate(ware, dep, file);
		} catch (NoSuchFieldException | SecurityException | NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 修改药品配料的方法
	 * @author xym
	 * @param dep
	 * @param wareId
	 * @return
	 */
	@RequestMapping("addDep")
	@ResponseBody
	public SsmMessage addDep(String dep,String wareId){
		try {
			return wareService.addDep(dep, wareId);
		} catch (NoSuchFieldException | SecurityException | NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 把上传的Excel导入到数据库
	 * @author zengren
	 * @param file
	 * @return
	 */
	@RequestMapping(value="uploadExcel",method = RequestMethod.POST)  
    @ResponseBody  
    public SsmMessage uploadExcel(@RequestParam(value="file",required = false)MultipartFile file){  
		SsmMessage mes = new SsmMessage();
		String result = wareService.readExcelFile(file);
        if("1".equals(result)){
        	mes.setMes(SsmMessage.SUCCESS_MES);
        	mes.setState(SsmMessage.SUCCESS);
        }else{
        	mes.setMes(SsmMessage.ERROR_MES);
        	mes.setState(SsmMessage.ERROR);
        }
        return mes;  
    }
	
	
	/**
	 * 将当前列表的数据导出为Excel
	 * @author zengren
	 * @param jsonStr
	 * @param response
	 */
	@RequestMapping(value="downExcel",method = RequestMethod.POST)
	public void downExcel(String jsonStr, HttpServletResponse response){
		
		JSONObject json = JSONObject.parseObject(jsonStr);  
		List<WareVo> jsonArray = (List<WareVo>) JSONArray.parseArray(json.getString("data"), WareVo.class);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Iterator<WareVo> iterator = jsonArray.iterator(); iterator.hasNext();) {
			WareVo w = (WareVo) iterator.next();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("wareName", w.getWareName());
			map.put("wareFactory", w.getWareFactory());
			map.put("wareMoney", w.getWareMoney());
			map.put("norma", w.getNorma());
			map.put("typeName", w.getTypeName());
			map.put("endTi", w.getEndTi());
			map.put("measure", w.getMeasure());
			String dos = "";
			List<BeyondDep> li = w.getDepList();
			for(int i=0; i<li.size(); i++){
				BeyondDep dep = li.get(i);
				dos += dep.getDosName()+":"+dep.getDosCount()+"（克）,";
			}
			dos = dos.substring(0, dos.length()-1);
			map.put("dos", dos);
			list.add(map);
		}
		String jsonText = JSONArray.toJSONString(list);
		JSONArray array = JSONArray.parseArray(jsonText);
		Map<String, String> headMap = new LinkedHashMap<String, String>();
		headMap.put("typeName", "种类");
		headMap.put("wareName", "药品名称");
		headMap.put("wareFactory", "生产厂家");
		headMap.put("norma", "规格");
		headMap.put("measure", "计量单位");
		headMap.put("wareMoney", "售价");
		headMap.put("endTi", "保质期（月）");
		headMap.put("dos", "配料");
		ExcelUtil.downloadExcelFile("药品列表", headMap, array, response);
	}
	
}
